package org.gxuwz.core.dao.impl;
import org.gxuwz.core.entity.TUserRole;
import org.gxuwz.core.entity.TUser;

import java.util.ArrayList;
import java.util.List;

import org.gxuwz.core.dao.IUserDao;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository("UserDao")
public class UserDao extends BaseDao implements IUserDao {
	 @Autowired
	 private SessionFactory sessionFactory;
	  public Session getSession(){
		  Session session=sessionFactory.openSession();
		  return session;
	  }
	//用户登陆时验证用户信息
	  @Override
	 public boolean validateTUser(String userid,String pwd){
		 Session session=getSession();
		 //String hql=" from TUser where userid=? and pwd=? ";
		 String hql="select * from t_user where userid=? and pwd=? ";
		 List<TUser> list = new ArrayList<TUser>();
		// Query query = session.createQuery(hql);
		 Query query = session.createSQLQuery(hql).addEntity(TUser.class);
		 query.setParameter(0, userid);
		 query.setParameter(1, pwd);
		 list = query.list();
	     if(list.size()!=0){
				session.close();
				return true;
			}
			session.close();
			System.out.println("123456798");
			return false;
		}
	  
	 
   //获取用户角色
	  @Override
	 public TUserRole getRoleId(String userid,String roleid){
		  TUserRole userRole = null;
		 Session session=getSession();
		 String hql="select * from t_user_role where userid=? and roleid=? ";
		 System.out.println(userid+roleid+"fffffffffffffffffff");
		 List<TUserRole> list = new ArrayList<TUserRole>();
		 Query query = session.createSQLQuery(hql).addEntity(TUserRole.class);
		 query.setParameter(0, userid);
		 query.setParameter(1, roleid);
		 list = query.list();
		 if(list.size()!=0){
				userRole = list.get(0);
				 System.out.println(list.get(0).toString()+"pppppppppppppppppppppp");
			}
		
		    session.close();
			return userRole;
	     

	 }
	  //向数据库写入用户信息
	  @Override
		 public void saveTUser(TUser User){
			// TODO Auto-generated method stub
				Session session=getSession();
				Transaction tx=session.beginTransaction();
				session.save(User);
				tx.commit();
				session.close(); 
		 }
		//向数据库写入用户角色信息
		 @Override
		 public void saveTUserRole(TUserRole UserRole){
			 Session session=getSession();
				Transaction tx=session.beginTransaction();
				session.save(UserRole);
				tx.commit();
				session.close(); 
		 }
		 
		@Override
		public List<TUser> UserList() {
			//String hql = " from TUser where 1=1";
			String hql = "select * from t_user where 1=1";
			List<TUser> list = new ArrayList<TUser>();
			Session session = getSession();
			Query query = session.createSQLQuery(hql);
			list = query.list();
			return list;
		}
		@Override
		public List<TUserRole> UserRoleList() {
			String hql = "from TUserRole where 1=1";
			List<TUserRole> lists = new ArrayList<TUserRole>();
			Session session = getSession();
			Query query = session.createQuery(hql);
			lists = query.list();
			return lists;
		}
		@Override
		public String getTUserRole(String userid, String roleid) {
			// TODO Auto-generated method stub
			return null;
		}
		
		
		 
}
